package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.log4j.Logger;

public class WordDAO {
	Logger LOG = Logger.getLogger("WordDAO");
	/**
	 * 
	 * @param word word want to insert to database
	 * @param meaning meaning of word
	 * @param dictID id of dictionary want to insert into
	 * @param status
	 * @return true if success
	 */
	public boolean addWord(String word, String meaning, int dictID, int status) {
		Connection conn = util.Controller.getConnection();
		PreparedStatement pstat = null;
		String query = "insert into m_word(m_word, m_meaning, m_dictID, m_status) values (?, ?, ?, ?)";
		try {
			pstat = conn.prepareStatement(query);
			pstat.setString(1, word);
			pstat.setString(2, meaning);
			pstat.setInt(3, dictID);
			pstat.setInt(4, status);
			pstat.execute();
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
//	public static void main(String args[]){
//		WordDAO dao = new WordDAO();
//		System.out.println(dao.deleteWord(1));
//	}
	
	/**
	 * function delete word
	 * @param id id of word
	 * @return true if success
	 */
	public boolean deleteWord(int id) {
		
		Connection conn = util.Controller.getConnection();
		PreparedStatement pstat = null;
		String query = "delete from m_word where m_id = ?";
		try {
			pstat = conn.prepareStatement(query);
			pstat.setInt(1, id);
			pstat.execute();
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
	/**
	 * function update meaning of word
	 * @param id id of word
	 * @param meaning meaning of word want to update
	 * @return true if success
	 */
	public boolean updateWord(int id, String meaning) {
		Connection conn = util.Controller.getConnection();
		PreparedStatement pstat = null;
		String query = "update m_word set m_meaning = ? where m_id = ?";
		try {
			pstat = conn.prepareStatement(query);
			pstat.setString(1, meaning);
			pstat.setInt(2, id);
			pstat.execute();
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
	/**
	 * function update status of word
	 * @param id id of word
	 * @param status status want to update
	 * @return true if success.
	 */
	public boolean updateWord(int id, int status) {
		Connection conn = util.Controller.getConnection();
		PreparedStatement pstat = null;
		String query = "update m_word set m_status = ? where m_id = ?";
		try {
			pstat = conn.prepareStatement(query);
			pstat.setInt(1, status);
			pstat.setInt(2, id);
			pstat.execute();
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
}
